<?php

namespace App\Http\Controllers;

use App\Models\Correction;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class CorrectionController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $data = Correction::requestCriteria($request)->paginate($request->query('limit'));
        return $this->respondSuccess($data);
    }

    public function correction_factory($type)
    {
        for ($i=0; $i < 60; $i++) { 
            $interval = $i * 5;
            $time = date("Y-m-d H:i:00",strtotime("-$interval minute"));
            $bms = Arr::random([90, 91, 92, 92, 95, 96, 96, 97, 97, 97, 99, 100]);
            $diff =  Arr::random([0,-1, 0, 2, 2, 3, -2, 1, 1, 3, 2, -1, 2]);
            $algorithm = $bms + $diff ;
            $item = [
                'time' => $time,
                'bms' => $bms,
                'algorithm' => $algorithm,
                'diff' => $diff,
            ];
            $error_curve[$i] = $item;
        }
        $batteries = [ 
            ['rank' => 1, 'name' => "0#电池模组", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 2, 'name' => "1#电池模组", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 3, 'name' => "2#电池模组", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 4, 'name' => "3#电池模组", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 5, 'name' => "4#电池模组", 'deviation' => "5%", 'loss_power' => "12kwh"]
        ];
        $battery_cells = [ 
            ['rank' => 1, 'name' => "0#-0号", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 2, 'name' => "0#-1号", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 3, 'name' => "0#-2号", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 4, 'name' => "0#-3号", 'deviation' => "5%", 'loss_power' => "12kwh"],
            ['rank' => 5, 'name' => "0#-4号", 'deviation' => "5%", 'loss_power' => "12kwh"]
        ];
        $deviation = ['充电截至SOC' => '95%', '放电截至SOC' => '15.0%', '最大SOC误差' => '5%', '损失电量估算' => '14kWh'];
        $deviation_rank = [ '电池模组SOC误差排行' => $batteries, '电芯SOC误差排行' => $battery_cells];
        $advice = ['建议充电截至SOC' => '96.0%', '建议放电截至SOC' => '5.0%', '检查更换BMU建议' => 0 ];
        $data = [
            'dev_type' => 'App\Models\ESCluster', 
            'deviation' => $deviation,
            'type' => $type,
            'deviation_rank' => $deviation_rank,
            'advice' => $advice,
            'error_curve' => $error_curve,
            'remarks' => '备注'
        ];
        if ($type == 'SOH校正'){
            $str = json_encode($data);
            $new = str_replace("SOC", "SOH", $str);
            $data = json_decode($new, TRUE);
        }
        return $data;
        
    }
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'dev_id' => 'required',
            'type' => 'required'
        ]);
        $type = $request->input('type');
        $data = $this->correction_factory($type);
        $data['dev_id'] = $request->input('dev_id');
        $correction = Correction::create($data);
        return $this->respondSuccess($correction);
    }

    public function show_last(Request $request)
    {
        $this->validate($request, [
            'dev_id' => 'required',
            'type' => 'required',
        ]);
        $dev_id = $request->input('dev_id');
        $type = $request->input('type');
        $data = Correction::where('dev_id', $dev_id)->where('type', $type)->orderByDesc('created_at')->first();
        return $this->respondSuccess($data);
    }
    /**
     * Display the specified resource.
     */
    public function show(Correction $correction)
    {
        return $this->respondSuccess($correction);
    }


    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Correction $correction)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Correction $correction)
    {
        //
    }
}
